package services

import (
	"github.com/zw2582/zm_go_lib/helpers"
	"kitalk/models"
)

//查询用户历史消息
func CustomerMessageHistory(uid, lastId uint, size int) (result []helpers.HArr) {
	result = make([]helpers.HArr, 0)

	//查询会话id
	chart := models.ChartGetByUid(uid)
	if chart.Id == 0 {
		return
	}

	//查询聊天数据
	records := models.ChartRecordHistory(chart.Id, lastId, size)
	if len(records) == 0 {
		return
	}

	//获取发送人信息
	uids := make([]interface{}, 0)
	for _, val := range records  {
		uids = append(uids, val.Uid)
	}
	users := models.UserListInId(uids)
	userByIds := make(map[uint]models.User)
	for _, val := range users {
		userByIds[val.Id] = val
	}

	//组装结果
	for _, val := range records {
		u := userByIds[val.Uid]
		result = append(result, helpers.HArr{
			"uindex":u.UUID,
			"uface":u.Avatar,
			"uname":u.Name,
			"date":PrettyCreatedAt(val.CreatedAt),
			"content":val.Content,
			"record_id":val.Id,
		})
	}
	return result
}